package by.ins.app.commands;

import java.util.List;

import by.ins.app.App;
import by.ins.app.AppContext;
import by.ins.life.ILife;
import by.ins.life.evolution.ILifeEvoluter;
import by.ins.life.simpleimpl.LifeEvolutor;
import by.yativem.commons.console.ConsoleCommandBean;
import by.yativem.commons.task.AbstractCallableTaskCommand;
import by.yativem.commons.task.AbstractManagedTask;
import by.yativem.commons.task.MangetTaskCommand;

public class ManagedTaskCommandBulder {
	/*
	 * public static class CommandConstants{ public static final String COMMAND_
	 * = ""; public static final String COMMAND_PAUSE = "paused"; public static
	 * final String COMMAND_CONTINUE = "continue"; public static final String
	 * COMMAND_CANCEL = "cancel"; public static final String
	 * COMMAND_LIFEEVOLUTOR = "ls_lifes";
	 * 
	 * 
	 * }
	 */
	@SuppressWarnings("rawtypes")
	public static AbstractCallableTaskCommand<?,? extends AbstractManagedTask> buildTaskCommand(ConsoleCommandBean cb) {
		AbstractCallableTaskCommand command = null;		
		
			if ("paused".equals(cb.getCommandName())) {
				
				command = MangetTaskCommand.getPausedTaskCommand();
			}


			if ("continue".equals(cb.getCommandName() )) {
				command = MangetTaskCommand.getContinueTaskCommand();
			}

			if ("cancel".equals(cb.getCommandName())) {
				command = MangetTaskCommand.getCancelTaskCommand();
			}
			
			if ("ls_lifes".equals(cb.getCommandName())){
				return new AbstractCallableTaskCommand<ILifeEvoluter[], App>() {

					@Override
					protected ILifeEvoluter[] doCommand() throws Exception {
						StringBuilder sb = new StringBuilder();
						List<ILifeEvoluter> lifes =  AppContext.getDefinedLifeEvolutor();
						for(ILifeEvoluter life : lifes){
							sb.append(life.toString()).append("\n");
						}
						
						return  (ILifeEvoluter[]) lifes.toArray(new LifeEvolutor[0] );
					}
				};
			}
			
			if (AppCommandConstants.COMMAND_START_TASK.equals(cb.getCommandName())) {
				final ConsoleCommandBean commandBean = cb;
				
				/*
				 *  retrun task id
				 */
				return new AbstractCallableTaskCommand<Long, App>() {

					@Override
					protected Long doCommand() throws Exception {					
						
						AbstractManagedTask taskToRun = null; /*task.getTasksEngine().getManagedTask(
								commandBean.getParam(AppCommandConstants.COMMAND_PROP_TASK_NAME));
								*/
						String tName  =commandBean.getParam(AppCommandConstants.COMMAND_PROP_START_TASK_NAME);
						taskToRun = (AbstractManagedTask) AppContext.getDefiendLifeEvoluter(tName);
						
						
						if(taskToRun == null){
							throw new IllegalArgumentException("Wrong task name passed. Task name :" + tName);
						}
						
						this.task.getTasksEngine().processTask(0, taskToRun);						
						return taskToRun.getTaskId();
					}
				};				
			}
			
			
			
			if (AppCommandConstants.COMMAND_EVOLUTE.equals(cb.getCommandName())) {
				final ConsoleCommandBean commandBean = cb;
				
				/*
				 *  retrun task id
				 */
				return new AbstractCallableTaskCommand<Boolean, LifeEvolutor>() {

					@Override
					protected Boolean doCommand() throws Exception {						
						task.evolute();											
						return true;
					}
				};				
			}
			
			if (AppCommandConstants.COMMAND_GET_LIFE.equals(cb.getCommandName())) {
				//final ConsoleCommandBean commandBean = cb;
				
				/*
				 *  retrun task id
				 */
				return new AbstractCallableTaskCommand<ILife, LifeEvolutor>() {

					@Override
					protected ILife doCommand() throws Exception {						
						ILife result =  task.getLife();											
						return result;
					}
				};				
			}

			
	//	}

				
		return command;
		
	}
}
